import {getPlaceclearanceList, getPlaceclearanceById, addPlaceclearance, updatePlaceclearance, deletePlaceclearanceById } from '../../services/basedata/placeclearance';
import {parse} from 'qs';
import { message } from 'antd';

export default {
    namespace: 'placeclearance',
    state: {
        search: { // 查询条件
            currencyCode: '',
            status: '1'
        },
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list: [],
        editModel: { // 编辑视图
            showStatus: false, // 默认不显示
            confirmLoading: false, // 确认按钮loading状态
            title: '创建海关口岸', // 标题
            type: 'add',
            form: {
                placeCode: '',
                placeName: '',
                placeCity: '',
                placeTransfertype: ''
            }
        }
    },
    reducers: {
        /**
         * 设置查询条件
         *
         * @param state
         * @param actio
         * @returns {{search}}
         */
        setSearch(state, action) {
            return {...state, search: action.payload.search};
        },

        /**
         * 是否显示
         *
         * @param state
         * @param action
         * @returns {{showStatus: (boolean|*)}}
         */
        setEditModel(state, action) {
            let payload = action.payload;

            let newState = {...state.editModel};
            if(payload.showStatus != undefined) {
                newState.showStatus = payload.showStatus;
            }
            if(payload.title != undefined) {
                newState.title = payload.title;
            }
            if(payload.confirmLoading != undefined) {
                newState.confirmLoading = payload.confirmLoading;
            }
            if(payload.type != undefined) {
                newState.type = payload.type;
            }
            if(payload.form != undefined) {
                newState.form = payload.form;
            }

            return {...state, editModel: {...newState}};
        },
        /**
         * 设置数据列表
         *
         * @param state
         * @param action
         */
        setList(state, { payload }) {
            return {...state, list: payload.list, page: payload.page}
        },

        /**
         * 设置页码
         *
         * @param state
         * @param payload
         * @returns {{page: *}}
         */
        setPage(state, { payload }) {
            return {...state, page: payload.page}
        }
    },
    effects: {

        /**
         * 分页获取海关口岸
         * @param call
         * @param put
         * @param select
         */
        *getPlaceclearanceList({}, { call, put, select }) {
            const search = yield select(state => state.placeclearance.search);
            const page = yield select(state => state.placeclearance.page);

            const res = yield call(getPlaceclearanceList, {placeCode: search.placeCode, placeName: search.placeName , page  });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                yield put({
                    type: 'setList',
                    payload: {
                        list: content.list,
                        page: content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 删除海关口岸
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *deletePlaceclearanceById({payload}, {call, put, select}) {
            const res = yield call(deletePlaceclearanceById, {id: payload.id});
            if (res.data.resultCode === "ok") {
                message.info("删除成功！");

                yield put({
                    type: "getPlaceclearanceList"
                });
            } else {
                message.error(res.data.errMessage);
            }

        },


        /**
         * 获取海关口岸详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getPlaceclearanceById({ payload }, { call, put, select }) {
            const res = yield call(getPlaceclearanceById, { id: payload.id});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: true,
                        confirmLoading: false,
                        title: "修改海关口岸",
                        type: 'update',
                        form: {
                            id:  content.id,
                            placeCode: content.placeCode,
                            placeName: content.placeName,
                            placeCity: content.placeCity,
                            placeTransfertype: content.placeTransfertype

                        }
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 修改海关口岸
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *updatePlaceclearance({ payload }, { call, put, select }) {
            const res = yield call(updatePlaceclearance, payload);
            if (res.data.resultCode === "ok") {
                message.info("修改成功！重新登录后生效");
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: false
                    }
                });
                yield put({
                    type: 'getPlaceclearanceList'
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 增加海关口岸
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *addPlaceclearance({ payload }, { call, put, select }) {
            const res = yield call(addPlaceclearance, payload);
            if (res.data.resultCode === "ok") {
                message.info("增加成功！");
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: false
                    }
                });
                yield put({
                    type: 'getPlaceclearanceList'
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

    },
    subscriptions: {
        setup({history, dispatch}) {
            return history.listen(({pathname}) => {
                if(pathname.includes('basedata/placeclearance')) {
                    dispatch({
                        type: 'getPlaceclearanceList'
                    });
                }

            });
        }
    }
};
